home *** CD-ROM | disk | FTP | other *** search
- Path: newsfeed.internetmci.com!iol!usenet
- From: David Byrden <Goyra@iol.ie>
- Newsgroups: comp.lang.c++
- Subject: Re: STL: Finding an object in a set of Pointers
- Date: Fri, 12 Apr 1996 19:12:07 +0100
- Organization: Ireland On-Line
- Message-ID: <316E9CF7.1B52@iol.ie>
- References: <4klmjm$kul@access4.digex.net>
- NNTP-Posting-Host: dialup-348.dublin.iol.ie
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.01 (Win95; I)
-
- Mr. Blue wrote:
- >
- > I am using an STL set to contain *pointers* to objects.
- >
- > Problem: I want to find out if an object I just created is
- > logically contained in the set.
- >
- > The find() function will tell me whether the same *address*
- > is already in the set, but I need to follow the pointers and
- > invoke operator==() on the objects.
- >
- > Is there an elegant way I can do this without writing my
- > own loop, de-referencing, and comparing?
-
- struct derefer_equal
- {
- const Thing * myptr ;
- derefer_equal( const Thing * p ) : myptr(p) {}
- bool operator==( const Thing*& pr )
- {
- return (*myptr) == (*pr) ;
- }
- } ;
-
-
-
- Thing t1 ;
- vector<Thing*> vt ;
- find_if( vt.begin(), vt.end(), derefer_equal( &t1 ) ) ;
-
- David
-